import time
from selenium import webdriver


# 登陆网站
def zhtj_login():
    drivers = webdriver.Chrome()
    drivers.get('https://zhtj.youth.cn/zhtj/')
    user_id = '410527198004140036'
    user_pw = '227718Lk'
    drivers.find_element_by_id('loginId').send_keys(user_id)
    drivers.find_element_by_id('password').send_keys(user_pw)
    user_yzm = input('Please input your yanzhengma:')
    drivers.find_element_by_id('yzm').send_keys(user_yzm)
    drivers.find_element_by_id('subBut').click()
    return drivers


# 进入转出菜单
def enter_out_gui(drivers):
    drivers.find_element_by_xpath('/html/body/div/div/div[2]/div[1]/div/div[2]/ul/li[9]/div').click()
    time.sleep(5)
    # print(drivers.find_element_by_class_name("el-menu-item"))
    # drivers.find_elements_by_class_name("el-menu-item")[0].click()
    drivers.find_element_by_xpath("/html/body/div/div/div[2]/div[1]/div/div[2]/ul/li[9]/ul/li[1]").click()
    # drivers.find_element_by_xpath("/html/body/div/div/div[2]/div[1]/div/div[2]/ul/li[6]/ul/li[1]").click()
    # time.sleep(2)
    # drivers.find_element_by_id('tab-second').click()
    # time.sleep(2)
    # drivers.find_element_by_xpath(
    #     '/html/body/div[1]/div/div[2]/div[2]/div/div[2]/div[1]/div[2]/div[3]/div[1]/button').click()
    # time.sleep(2)
    return drivers


# 选择团支部
def select_league(drivers):
    #循环班级
    for class_id in range(1,7):
        time.sleep(2)
        #转出菜单
        drivers.find_element_by_id('tab-second').click()
        time.sleep(2)
        #转出按钮
        drivers.find_element_by_xpath(
            '/html/body/div[1]/div/div[2]/div[2]/div/div[2]/div[1]/div[2]/div[3]/div[1]/button').click()
        time.sleep(2)
        #选择班级
        drivers.find_element_by_id('outLeague').click()
        time.sleep(3)
        #选择班级
        sel_league = '2018级' + str(class_id) + '班'
        drivers.find_element_by_xpath(
            '/html/body/div[1]/div/div[2]/div[2]/div/div[4]/div/div[2]/div[1]/div[1]/div/input').send_keys(sel_league)
        time.sleep(1)
        drivers.find_element_by_xpath('/html/body/div[1]/div/div[2]/'
                                      'div[2]/div/div[4]/div/div[2]/div[1]/div[2]/button').click()
        time.sleep(3)
        drivers.find_element_by_xpath('/html/body/div[1]/div/div[2]/div[2]/div/div[4]/div/div[2]/'
                                      'div[2]/div[3]/table/tbody/tr/td[2]/div/button').click()
        time.sleep(2)
        #提取团员数
        total_member=drivers.find_elements_by_id('outPerson')
        for i in range(1, 10):
            # 重新进入界面
            time.sleep(2)
            drivers.find_element_by_id('tab-second').click()
            time.sleep(2)
            drivers.find_element_by_xpath(
                '/html/body/div[1]/div/div[2]/div[2]/div/div[2]/div[1]/div[2]/div[3]/div[1]/button').click()
            time.sleep(2)
            drivers.find_element_by_id('outLeague').click()
            time.sleep(3)
            sel_league = '2018级' + str(class_id) + '班'
            drivers.find_element_by_xpath(
                '/html/body/div[1]/div/div[2]/div[2]/div/div[4]/div/div[2]/div[1]/div[1]/div/input').send_keys(sel_league)
            time.sleep(1)
            drivers.find_element_by_xpath('/html/body/div[1]/div/div[2]/'
                                          'div[2]/div/div[4]/div/div[2]/div[1]/div[2]/button').click()
            time.sleep(3)
            drivers.find_element_by_xpath('/html/body/div[1]/div/div[2]/div[2]/div/div[4]/div/div[2]/'
                                          'div[2]/div[3]/table/tbody/tr/td[2]/div/button').click()
            time.sleep(2)
            print(len(drivers.find_elements_by_xpath('/html/body/div[2]/div[1]/div[1]/ul/li')))
            time.sleep(1)
            print(i)
            if i == 1:
                j = 0
                # print(drivers.find_elements_by_xpath('/html/body/div[2]/div[1]/div[1]/ul/li')[i].get_property())
                print(drivers.find_elements_by_xpath('/html/body/div[2]/div[1]/div[1]/ul/li')[i].get_attribute('class'))
                if 'is-disabled' in drivers.find_elements_by_xpath('/html/body/div[2]/div[1]/div[1]/ul/li')[
                    i].get_attribute('class'):
                    drivers.find_element_by_xpath('/html/body/div[1]/div/div[2]/div[2]/div/div[1]/span[2]').click()
                    drivers.find_element_by_xpath(
                        '/html/body/div[1]/div/div[2]/div[2]/div/div[2]/div/div[2]/div/button[1]').click()
                    # /html/body/div[1]/div/div[2]/div[2]/div/div[2]/div/div[2]/div/button[1]
                    # /html/body/div/div/div[2]/div[2]/div/div[2]/div/div[2]/div/button[1]
                    continue
                drivers.find_elements_by_xpath('/html/body/div[2]/div[1]/div[1]/ul/li')[i].click()
            # /html/body/div[3]/div[1]/div[1]/ul/li[5]
            elif i == 9:
                j = 1
                drivers.find_elements_by_xpath('/html/body/div[3]/div[1]/div[1]/ul/li')[0].click()
            else:
                j = 1
                if 'is-disabled' in drivers.find_elements_by_xpath('/html/body/div[3]/div[1]/div[1]/ul/li')[
                    i].get_attribute('class'):
                    drivers.find_element_by_xpath('/html/body/div[1]/div/div[2]/div[2]/div/div[1]/span[2]').click()
                    drivers.find_element_by_xpath(
                        '/html/body/div[1]/div/div[2]/div[2]/div/div[2]/div/div[2]/div/button[1]').click()
                    # /html/body/div[1]/div/div[2]/div[2]/div/div[2]/div/div[2]/div/button[1]
                    continue
                drivers.find_elements_by_xpath('/html/body/div[3]/div[1]/div[1]/ul/li')[i].click()
            time.sleep(1)
            # 电话
            drivers.find_element_by_id('commonlyUsePhone').send_keys('15637216536')
            time.sleep(1)
            # 是否福建广东
            drivers.find_element_by_id('isForeignSelect').click()
            time.sleep(1)
            drivers.find_elements_by_xpath('/html/body/div[%d]/div[1]/div[1]/ul/li' % (3 + j))[1].click()
            # /html/body/div[4]/div[1]/div[1]/ul
            time.sleep(1)
            # 转出原因
            drivers.find_element_by_id('outReson').click()
            time.sleep(1)
            drivers.find_elements_by_xpath('/html/body/div[%d]/div[1]/div[1]/ul/li' % (4 + j))[3].click()
            # /html/body/div[5]/div[1]/div[1]/ul
            time.sleep(1)
            # 是否保留在原学校
            drivers.find_element_by_id('ofOriginalSchool').click()
            time.sleep(1)
            drivers.find_elements_by_xpath('/html/body/div[%d]/div[1]/div[1]/ul/li' % (5 + j))[1].click()
            # /html/body/div[6]/div[1]/div[1]/ul
            time.sleep(1)
            drivers.find_element_by_xpath('/html/body/div[6]/div/div[3]/button').click()
            # /html/body/div[6]/div/div[3]/button
            time.sleep(3)
            drivers.find_element_by_xpath('/html/body/div[1]/div/div[2]/div[2]/div/div[2]/div/div[2]/div/button[2]').click()
            time.sleep(5)


if __name__ == '__main__':
    drive = zhtj_login()
    time.sleep(3)
    drive = enter_out_gui(drive)
    time.sleep(3)
    select_league(drive)
